package com.nidu.demo.mail.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.mail.convertor.MailLogConvertor;
import com.nidu.demo.mail.dto.MailLogCO;
import com.nidu.demo.mail.gateway.MailLogGateway;
import com.nidu.demo.mail.model.MailLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 邮件发送日志单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MailLogSingleQryExe {

    private final MailLogGateway gateway;
    private final MailLogConvertor convertor;

    public SingleResponse<MailLogCO> execute(Long id) {
        // 1. 根据ID查询邮件账号
        MailLog mailLog = gateway.getById(id);
        if (mailLog == null) {
            throw new BizException(ErrorCodeConstants.MAIL_LOG_NOT_FOUND.getCode(), ErrorCodeConstants.MENU_NOT_FOUND.getMessage());
        }

        // 2. 转换为DTO返回
        MailLogCO mailLogCO = convertor.toClientObject(mailLog);
        return SingleResponse.of(mailLogCO);
    }

}
